package com.ruoyi.teacher.controller;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.core.constant.CacheConstants;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.redis.service.RedisService;
import com.ruoyi.teacher.api.domain.Campus;
import com.ruoyi.teacher.api.domain.Subject;
import com.ruoyi.teacher.service.CampusService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/subject")
public class SubjectController {
    @Autowired
    private CampusService campusService;
    @Autowired
    private RedisService redisService;
    @GetMapping
    public R<List<Subject>> loadSubjects() {
        if (!redisService.hasKey(CacheConstants.SUBJECT_KEY)) {
            List<Subject> subjects = campusService.loadSubjects();
            redisService.setCacheList(CacheConstants.SUBJECT_KEY, subjects);
            return R.ok(subjects);
        }else {
            List<Subject> subjects = BeanUtil.copyToList(redisService.getCacheList(CacheConstants.SUBJECT_KEY), Subject.class);
            return R.ok(subjects);
        }
    }
}
